package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AdminAgentInfo;
import com.ruoyi.system.service.IAdminAgentInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 代理信息Controller
 * 
 * @author ruoyi
 * @date 2020-11-11
 */
@RestController
@RequestMapping("/system/agentInfo")
public class AgentInfoController extends BaseController
{
    @Autowired
    private IAdminAgentInfoService adminAgentInfoService;

    /**
     * 查询代理信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:agentInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdminAgentInfo adminAgentInfo)
    {
        startPage();
        List<AdminAgentInfo> list = adminAgentInfoService.selectAdminAgentInfoListForAdmin(adminAgentInfo);
        return getDataTable(list);
    }

    /**
     * 导出代理信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:agentInfo:export')")
    @Log(title = "代理信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AdminAgentInfo adminAgentInfo)
    {
        List<AdminAgentInfo> list = adminAgentInfoService.selectAdminAgentInfoList(adminAgentInfo);
        ExcelUtil<AdminAgentInfo> util = new ExcelUtil<AdminAgentInfo>(AdminAgentInfo.class);
        return util.exportExcel(list, "agentInfo");
    }

    /**
     * 获取代理信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:agentInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(adminAgentInfoService.selectAdminAgentInfoById(id));
    }

    /**
     * 新增代理信息
     */
    @PreAuthorize("@ss.hasPermi('system:agentInfo:add')")
    @Log(title = "代理信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdminAgentInfo adminAgentInfo)
    {
        AjaxResult result =AjaxResult.success();
        int i = adminAgentInfoService.insertAdminAgentInfo(adminAgentInfo);
        if (!adminAgentInfo.getCommand().equals("") && !adminAgentInfo.getAgentName().equals("")) {
            if (i==0) {
                result=AjaxResult.error("代理名称已存在");
            }else  if (i==2) {
                result=AjaxResult.error("代理命令已重复，请重新输入");
            }
        }else{
            result=AjaxResult.error("代理名称或代理命令不能为空，请检查");
        }

        return result;
    }

    /**
     * 修改代理信息
     */
    @PreAuthorize("@ss.hasPermi('system:agentInfo:edit')")
    @Log(title = "代理信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdminAgentInfo adminAgentInfo)
    {
        AdminAgentInfo adminAgentInfo1 = adminAgentInfoService.selectAdminAgentInfoByCommandAndAgentId(adminAgentInfo);
        if (adminAgentInfo1==null) {
            return toAjax(adminAgentInfoService.updateAdminAgentInfo(adminAgentInfo));
        }else{
            AjaxResult result =AjaxResult.error("代理命令已重复");
            return result;
        }
    }

    /**
     * 删除代理信息
     */
    @PreAuthorize("@ss.hasPermi('system:agentInfo:remove')")
    @Log(title = "代理信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(adminAgentInfoService.deleteAdminAgentInfoByIds(ids));
    }
}
